import java.io.*;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import traductor.samLexer;
import traductor.samParser;
import traductor.javaLexer;
import traductor.javaParser;
import emulador.SAM;

public class Main {

	public static void main (String[] args) throws Exception{
		try {
			System.out.println ("Analizando fichero java");
			CharStream cs = new ANTLRFileStream(args[0]);
			javaLexer lex = new javaLexer(cs);
			CommonTokenStream tokens = new CommonTokenStream(lex);
			javaParser parser = new javaParser(tokens);
			parser.entrada();
			System.out.println("Creando el fichero con codigo intermedio para la maquina SAM ...");
			Thread.sleep(10000);	
            CharStream cs2 = new ANTLRFileStream("intermedio.txt", "UTF-8");
            samLexer samLex = new samLexer(cs2);
            CommonTokenStream tokens2 = new CommonTokenStream(samLex);
            samParser samPar = new samParser(tokens2);
            samPar.parseLabels();
            tokens2.reset();
            samPar.generateCode();
            System.out.println("Emulando la memoria de la maquina");
            SAM sam = new SAM(samPar.getCode());
            sam.run();

		 }catch(IOException e) {
			System.err.println("No existe el fichero");
		}
	}
}